﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_Recharge")]
    public class T_Recharge : IEntity<T_Recharge>
    {
        [Key]
        [Column(TypeName="int")]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string UserName { get; set; }

        [Column(TypeName="decimal")]
        public decimal RechargeValue { get; set; }
        public DateTime RechargeDate { get; set; }
        public int PayType { get; set; }

        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string Remark { get; set; }

        [Column(TypeName = "decimal")]
        public decimal Balance { get; set; }

        public override T_Recharge Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Recharge Find(System.Linq.Expressions.Expression<Func<T_Recharge, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Recharge.FirstOrDefault(where);
                if (item != null)
                {
                    this.Balance = item.Balance;
                    this.Id = item.Id;
                    this.PayType = item.PayType;
                    this.RechargeDate = item.RechargeDate;
                    this.RechargeValue = item.RechargeValue;
                    this.Remark = item.Remark;
                    this.UserName = item.UserName;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Recharge, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Recharge.Any(where);
            }
        }

        public override T_Recharge FindLast(System.Linq.Expressions.Expression<Func<T_Recharge, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Recharge.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.Balance = item.Balance;
                    this.Id = item.Id;
                    this.PayType = item.PayType;
                    this.RechargeDate = item.RechargeDate;
                    this.RechargeValue = item.RechargeValue;
                    this.Remark = item.Remark;
                    this.UserName = item.UserName;
                    return this;
                }
                return null;
            }
        }
    }
}
